package audio_testing;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class JudgeFormat {
              public static TranslateByteToDouble  JudgeFormatRight(File f) throws IOException
             {
            	  RandomAccessFile file=new RandomAccessFile(f,"r");
            	 
            	  if(file.read()<46){
            		  throw new IOException("Not usefull PCM");
            	  }
            	  file.seek(0);
            	  if(file.readInt()!=0x52494646){
            		  throw new IOException("Not RIFF");
            	  }
            	 file.seek(8);
            	 if(file.readInt()!=0x57415645){
           		  throw new IOException("Not WAVE");
           	     }
            	 file.seek(20);
            	 if(file.readShort()!=(short)0x0100){
            		 throw new IOException("Not PCM");
            	 }
            	 file.seek(22);
            	 if(file.readShort()!=(short)0x0100){
            		 throw new IOException("NumChannels has>1");
            	 }
            	 file.seek(24);
            	 if(file.readInt()!=0x44AC0000){
            		 throw new IOException("SampleRate is not 44100HZ ");
            	 }
            	 file.seek(34);
            	 if(file.readShort()!=0x1000){
            		 throw new IOException("BitsPerSample is not 16 bit");
            	 }
            	 file.seek(42);
                 int a = file.readByte() & 0xFF;
                 file.seek(43);
                 int b = (file.readByte() << 8) & 0xFF00;
                 file.seek(44);
                 int c = (file.readByte() << 16) & 0xFF0000;
                 file.seek(45);
                 int d = (file.readByte() << 24) & 0xFF000000;
                 int size = d | c | b | a;
                 if (size + 46 != f.length()) {
                     throw new IOException("The audio is broken");
                 }
            	  file.seek(46);
             	 TranslateByteToDouble test=new TranslateByteToDouble();
             	 byte[]PCMdata=new byte[size];
             	 file.seek(46);
                 file.read(PCMdata);
                 test.setData(PCMdata);
             	 return test;
              }
              
}
